<!--
 * @Descripttion: (房间日历表/lay_room_price)
 * @Author: (admin)
 * @Date: (2024-11-13)
-->
<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!-- 右边民宿 房型 数据区域 -->
      <el-col :span="4" :xs="24">
        <el-table v-if="refreshTable" :data="dataTreeList" border default-expand-all v-loading="loading" ref="tableRef"
          highlight-current-row @selection-change="handleSelectionChange" :default-expand-all="isExpandAll"
          row-key="value" @row-click="GetRoomSpec" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
          <el-table-column prop="label" :show-overflow-tooltip="true" />
        </el-table>
      </el-col>
      <el-col :lg="20" :xm="24">

        <el-table :data="layRoomSpecList" :row-class-name="rowLayRoomSpecIndex" highlight-current-row border
          @row-click="GetCalendarPrice" ref="LayRoomSpecRef">
          <!-- <el-table-column type="selection" width="40" align="center" /> -->
          <el-table-column label="套餐名称" align="center" prop="specName">
          </el-table-column>

          <el-table-column label="划线价" align="center" prop="linePrice">
          </el-table-column>
          <el-table-column label="日常价格" align="center" prop="dailyPrice">

          </el-table-column>
          <el-table-column label="周末价格" align="center" :precision="2" prop="weekPrice">

          </el-table-column>
          <el-table-column label="节假价格" align="center" prop="holidayPrice">

          </el-table-column>

          <!-- <el-table-column label="状态" align="center" prop="status" width="225">
            <template #default="scope">
              <el-select v-model="scope.row.status" placeholder="请选择状态">
                <el-option v-for="item in options.status" :key="item.dictValue" :label="item.dictLabel"
                  :value="item.dictValue"></el-option>
              </el-select>
            </template>
</el-table-column> -->
        </el-table>
        <!-- <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
          <el-form-item>
            <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
            <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
          </el-form-item>
        </el-form> -->
        <!-- 工具区域 -->
        <!-- <el-row :gutter="15" class="mb10">
         <el-col :span="1.5">
            <el-button type="primary" v-hasPermi="['layroomprice:add']" plain icon="plus" @click="handleAdd">
              {{ $t('btn.add') }}
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" :disabled="single" v-hasPermi="['layroomprice:edit']" plain icon="edit"
              @click="handleUpdate">
              {{ $t('btn.edit') }}
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" :disabled="multiple" v-hasPermi="['layroomprice:delete']" plain icon="delete"
              @click="handleDelete">
              {{ $t('btn.delete') }}
            </el-button>
          </el-col>
           <el-col :span="1.5">
            <el-button type="warning" plain icon="download" @click="handleExport" v-hasPermi="['layroomprice:export']">
              {{ $t('btn.export') }}
            </el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
        </el-row> -->
        <!-- <el-table :data="dataList" v-loading="loading" ref="table" border header-cell-class-name="el-table-header-cell"
          highlight-current-row @sort-change="sortChange" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column prop="status" label="Status" align="center" v-if="columns.showColumn('status')">
            <template #default="scope">
              <dict-tag :options="options.status" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column prop="isEdit" label="修改" align="center" v-if="columns.showColumn('isEdit')">
            <template #default="scope">
              <dict-tag :options="options.isEditOptions" :value="scope.row.isEdit" />
            </template>
          </el-table-column>
          <el-table-column prop="holiday" label="节假日" align="center" v-if="columns.showColumn('holiday')" />
          <el-table-column prop="typeId" label="1==日常 2==节假日 3==调休" align="center"
            v-if="columns.showColumn('typeId')" />
          <el-table-column prop="roomSpecId" label="RoomSpecId" align="center" :show-overflow-tooltip="true"
            v-if="columns.showColumn('roomSpecId')" />
          <el-table-column prop="statusName" label="StatusName" align="center" v-if="columns.showColumn('statusName')">
            <template #default="scope">
              <dict-tag :options="options.statusNameOptions" :value="scope.row.statusName" />
            </template>
          </el-table-column>
          <el-table-column prop="id" label="Id" align="center" :show-overflow-tooltip="true"
            v-if="columns.showColumn('id')" />
          <el-table-column prop="remark" label="Remark" align="center" :show-overflow-tooltip="true"
            v-if="columns.showColumn('remark')" />
          <el-table-column prop="priceTag" label="PriceTag" align="center" :show-overflow-tooltip="true"
            v-if="columns.showColumn('priceTag')" />
          <el-table-column prop="priceSub" label="PriceSub" align="center" v-if="columns.showColumn('priceSub')" />
          <el-table-column prop="price" label="Price" align="center" v-if="columns.showColumn('price')" />
          <el-table-column prop="weekNum" label="WeekNum" align="center" v-if="columns.showColumn('weekNum')" />
          <el-table-column prop="week" label="Week" align="center" :show-overflow-tooltip="true"
            v-if="columns.showColumn('week')" />
          <el-table-column prop="datesTag" label="DatesTag" align="center" :show-overflow-tooltip="true"
            v-if="columns.showColumn('datesTag')" />
          <el-table-column prop="dates" label="Dates" :show-overflow-tooltip="true"
            v-if="columns.showColumn('dates')" />
          <el-table-column label="操作" width="160">
            <template #default="scope">
              <el-button type="primary" size="small" icon="view" title="详情"
                @click="handlePreview(scope.row)"></el-button>
              <el-button type="success" size="small" icon="edit" title="编辑" v-hasPermi="['layroomprice:edit']"
                @click="handleUpdate(scope.row)"></el-button>
              <el-button type="danger" size="small" icon="delete" title="删除" v-hasPermi="['layroomprice:delete']"
                @click="handleDelete(scope.row)"></el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
          @pagination="getList" /> -->
        <el-divider content-position="center">日历价格表</el-divider>
        <el-calendar :range="[state.startDate, state.endDate]">
          <template #date-cell="{ data }">
            <div v-for="(item, index) in textContent(data.day)" :key="index" >
              <span style="font-weight:bold">{{ dayjs(data.day).format("DD") }}</span>
              <e-row v-if="item.holidayName != ''" style="position: relative;">
                <el-col class="center" style="position: absolute;left:112px;top:-8px;">
                  <el-tag type="warning" class="tag">
                    <el-row>
                      <el-col :span="15" class="tag2" style="text-align: left;">
                        <span style="color: red;font-weight: bold;">{{ item.holidayName }}</span>
                      </el-col>
                    </el-row>
                  </el-tag>
                </el-col>
              </e-row>

              <el-row style="text-align: center !important;">
                <el-col :span="12" class="center">
                  <span>￥</span><span class="center2" style="">
                    {{ item.price }}</span></el-col>
                <el-col :span="1" class="center"></el-col>
                <el-col :span="11" class="center">
                  <el-button type="primary" icon="el-icon-edit" @click="handleUpdate(item.id, item.roomSpecId)"
                    circle></el-button>
                </el-col>
                <el-col :span="24" class="left" style="color: green;margin-top:5px;font-size:13px;text-align: left;">
                  库 {{ item.stockNum? item.stockNum:0 }} | 定 {{ item.settledNum?item.settledNum:0
                  }} |<span style="color: red;"> 余 {{ item.surplusNum?item.surplusNum:0 }}</span></el-col>

              </el-row>
            </div>
          </template>
        </el-calendar>

      </el-col>
    </el-row>

    <el-dialog :title="title" width="20%" :lock-scroll="false" v-model="open">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">

          <!-- <el-col :lg="12">
            <el-form-item label="Status" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio v-for="item in options.status" :key="item.dictValue" :value="item.dictValue">
                  {{ item.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="修改" prop="isEdit">
              <el-radio-group v-model="form.isEdit">
                <el-radio v-for="item in options.isEditOptions" :key="item.dictValue" :value="item.dictValue">
                  {{ item.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col> -->

          <!-- <el-col :lg="12">
            <el-form-item label="节假日" prop="holiday">
              <el-input v-model.number="form.holiday" placeholder="请输入节假日" />
            </el-form-item>
          </el-col> -->

          <!-- <el-col :lg="12">
            <el-form-item label="1==日常 2==节假日 3==调休" prop="typeId">
              <el-input v-model.number="form.typeId" placeholder="请输入1==日常 2==节假日 3==调休" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="RoomSpecId" prop="roomSpecId">
              <el-input v-model="form.roomSpecId" placeholder="请输入RoomSpecId" />
            </el-form-item>
          </el-col> -->

          <!-- <el-col :lg="12">
            <el-form-item label="UpdateBy" prop="updateBy">
              <el-input v-model="form.updateBy" placeholder="请输入UpdateBy" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="UpdateTime" prop="updateTime">
              <el-date-picker v-model="form.updateTime" type="datetime" placeholder="选择日期时间"
                value-format="YYYY-MM-DD HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="CreateBy" prop="createBy">
              <el-input v-model="form.createBy" placeholder="请输入CreateBy" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="CreateTime" prop="createTime">
              <el-date-picker v-model="form.createTime" type="datetime" placeholder="选择日期时间"
                value-format="YYYY-MM-DD HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col> -->

          <!-- <el-col :lg="12">
            <el-form-item label="StatusName" prop="statusName">
              <el-radio-group v-model="form.statusName">
                <el-radio v-for="item in options.statusNameOptions" :key="item.dictValue" :value="item.dictValue">
                  {{ item.dictLabel }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col> -->

          <!-- <el-col :lg="12">
            <el-form-item label="Id" prop="id">
              <el-input v-model="form.id" placeholder="请输入Id" :disabled="opertype != 1" />
            </el-form-item>
          </el-col> -->

          <!-- <el-col :lg="12">
            <el-form-item label="PriceTag" prop="priceTag">
              <el-input v-model="form.priceTag" placeholder="请输入PriceTag" />
            </el-form-item>
          </el-col> -->

          <!-- <el-col :lg="12">
            <el-form-item label="PriceSub" prop="priceSub">
              <el-input v-model="form.priceSub" placeholder="请输入PriceSub" />
            </el-form-item>
          </el-col> -->

          <el-col :lg="24">
            <el-form-item label="价格" prop="price">
              <el-input v-model="form.price" type="number" :min=1 placeholder="价格" />
            </el-form-item>
          </el-col>
          <el-col :lg="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="备注" />
            </el-form-item>
          </el-col>
          <!-- <el-col :lg="12">
            <el-form-item label="WeekNum" prop="weekNum">
              <el-input v-model.number="form.weekNum" placeholder="请输入WeekNum" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="Week" prop="week">
              <el-input v-model="form.week" placeholder="请输入Week" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="DatesTag" prop="datesTag">
              <el-input v-model="form.datesTag" placeholder="请输入DatesTag" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="Dates" prop="dates">
              <el-date-picker v-model="form.dates" type="datetime" placeholder="选择日期时间"
                value-format="YYYY-MM-DD HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="layroomprice">
import {
  listLayRoomPrice,
  addLayRoomPrice, delLayRoomPrice,
  updateLayRoomPrice, getLayRoomPrice, LayRoomPriceCalendar
}
  from '@/api/business/layroomprice.js'

import { GetTreeLayHouseRoom } from '@/api/business/layhouse.js'
import { getLayRoom } from '@/api/business/layroom.js'
import { dayjs } from 'element-plus';
import { min, range } from 'xe-utils';
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: 'CreateTime',
  sortType: 'desc',
})
const columns = ref([
  { visible: true, align: 'center', type: 'dict', prop: 'status', label: 'Status', showOverflowTooltip: true, dictType: 'status' },
  { visible: true, align: 'center', type: 'dict', prop: 'isEdit', label: '修改' },
  { visible: true, align: 'center', type: '', prop: 'holiday', label: '节假日' },
  { visible: true, align: 'center', type: '', prop: 'typeId', label: '1==日常 2==节假日 3==调休' },
  { visible: true, align: 'center', type: '', prop: 'roomSpecId', label: 'RoomSpecId', showOverflowTooltip: true },
  { visible: false, align: 'center', type: 'dict', prop: 'statusName', label: 'StatusName', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'id', label: 'Id', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'remark', label: 'Remark', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'priceTag', label: 'PriceTag', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'priceSub', label: 'PriceSub' },
  { visible: false, align: 'center', type: '', prop: 'price', label: 'Price' },
  { visible: false, align: 'center', type: '', prop: 'weekNum', label: 'WeekNum' },
  { visible: false, align: 'center', type: '', prop: 'week', label: 'Week', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'datesTag', label: 'DatesTag', showOverflowTooltip: true },
  { visible: false, align: 'center', type: '', prop: 'dates', label: 'Dates', showOverflowTooltip: true },
  //{ visible: false, prop: 'actions', label: '操作', type: 'slot', width: '160' }
])
const total = ref(0)
const dataList = ref([])
const dataTreeList = ref([])
const layRoomSpecList = ref([])
const refreshTable = ref(true)
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


var dictParams = [
  "status",
]

proxy.getDicts(dictParams).then((response) => {
  response.data.forEach((element) => {
    state.options[element.dictType] = element.list
  })
})

function getList() {
  loading.value = true
  listLayRoomPrice(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

GetTreeLayHouseRoom().then(res => {
  const { code, data } = res
  if (code == 200) {
    dataTreeList.value = data
    GetRoomSpec(null, data.length > 0 ? data[0].children[0].value : '0')
  }
})
function GetRoomSpec(row, _id) {
  layRoomSpecList.value = null
  state.calendarData = null
  getLayRoom(row == null ? _id : row.value).then((res) => {
    const { code, data } = res
    if (code == 200) {
      layRoomSpecList.value = data.layRoomSpecNav
      GetCalendarPrice(null, data.layRoomSpecNav[0].id)
    }
  })
}

function GetCalendarPrice(row, _id) {
  LayRoomPriceCalendar(row == null ? _id : row.id).then((res) => {
    const { code, data } = res
    if (code == 200) {

      state.calendarData = data.roomPrices
      state.startDate = data.startDate
      state.endDate = data.endDate
      console.log(state.calendarData)
    }
  })
}
// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1
  multiple.value = !selection.length;
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  form: {},
  rules: {
    id: [{ required: true, message: "Id不能为空", trigger: "blur" }],
  },
  options: {
    // Status 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
    status: [],
    // 修改 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
    isEditOptions: [],
  },
  startDate: null,
  endDate: null,
  //测试数据
  calendarData: null,
  _specId: null
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel() {
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    status: null,
    isEdit: null,
    holiday: null,
    typeId: null,
    roomSpecId: null,
    updateBy: null,
    updateTime: null,
    createBy: null,
    createTime: null,
    statusName: null,
    id: null,
    remark: null,
    priceTag: null,
    priceSub: null,
    price: null,
    weekNum: null,
    week: null,
    datesTag: null,
    dates: null,
  };
  proxy.resetForm("formRef")
}

/**
 * 查看
 * @param {*} row
 */
function handlePreview(row) {
  reset()
  const id = row.id
  getLayRoomPrice(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '查看'
      opertype.value = 3
      form.value = {
        ...data,
      }
    }
  })
}

// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  title.value = '添加房间日历表'
  opertype.value = 1
}
// 修改按钮操作
function handleUpdate(_id, specId) {
  reset()
  //const id =row.id || ids.value
  state._specId = specId
  getLayRoomPrice(_id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '价格修改'
      opertype.value = 2

      form.value = {
        ...data,
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {

      if (form.value.id != undefined && opertype.value === 2) {
        updateLayRoomPrice(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          GetCalendarPrice(null, state._specId)
        })
      } else {
        addLayRoomPrice(form.value).then((res) => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          //getList()
          GetCalendarPrice(null, state._specId)
        })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.id || ids.value

  proxy
    .$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？', "警告", {
      confirmButtonText: proxy.$t('common.ok'),
      cancelButtonText: proxy.$t('common.cancel'),
      type: "warning",
    })
    .then(function () {
      return delLayRoomPrice(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    })
}



// 导出按钮操作
function handleExport() {
  proxy
    .$confirm("是否确认导出房间日历表数据项?", "警告", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    })
    .then(async () => {
      await proxy.downFile('/business/LayRoomPrice/export', { ...queryParams })
    })
}

handleQuery()



//处理日期获取后台数据动态渲染上去
const textContent = (date) => {
  //当前date是拿到上面日历组件当前的日期值 根据该值去筛选测试数据找到对应各个日期下对应的数据return出去
  if (state.calendarData != null) {
    return state.calendarData.filter((item) => {
      return date === dayjs(item.dates).format("YYYY-MM-DD");
    });
  }
};
</script>



<style scoped>
.is-selected {
  color: #1989fa;
}

:deep .el-calendar__body {
  padding: 4px 20px 35px;
}

:deep .el-calendar-table thead th {
  color: #ffab11;
  font-weight: bold;
  font-size: 25px;
}

.tag {
  display: flex;
  align-items: center;
  height: 2.5vh;
  justify-content: center;
}

.tag2 {
  display: flex;
  align-items: center;
  height: 2.5vh;
  font-size: 18px;
}



.aaa .datastyle {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 3rem;
  color: #353636;
  font-weight: 600;
}

:deep .el-calendar-table .el-calendar-day:hover {
  background-color: #faecd8;
}

:deep .el-calendar {
  --el-calendar-selected-bg-color: #faecd8;
}

.aaa .is-selected .datastyle {
  color: #ffab11;
}

.aaa .prev .datastyle {
  color: #c4c5c8;
}

.aaa .next .datastyle {
  color: #c4c5c8;
}

/* :deep .el-calendar {
  --el-calendar-cell-width: 130px;
} */

.center {
  display: flex;
  justify-content: center;
  align-items: center;
}

.center2 {
  display: flex;
  justify-content: center;
  align-items: center;
  color: #ffab11;
  margin-left: 5px;
  font-size: 20px;
}

:deep .el-calendar__header {
  justify-content: center;
}

.el-calendar__title {
  padding-right: inherit !important;
}

.el-calendar-table .el-calendar-day {
  padding: 4px !important;
}
</style>
